package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.user.RegisteredUserDto;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.ui.IView;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/11/19 22:23
 */
public class RegisterView extends IView {

    private RegisterCallback mCallback;

    public RegisterView(RegisterCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doRegister(String password, String nick, String phone) {
        RegisteredUserDto registeredUserDto = new RegisteredUserDto();
        registeredUserDto.setSecret(password);
        registeredUserDto.setNickname(nick);
        registeredUserDto.setPhone(phone);
        doSendRequest(EventConstants.Action.REGISTER, registeredUserDto.toJson());
    }

    @Override
    public void onMessageEventHandle(MessageEvent event) {
        Logger.print("RegisterView", event.toString());
        if (event.action() == EventConstants.Action.REGISTER) {
            RegisteredUserDto dto = null;
            if (checkIsSuccess(event.code(), event.data()))
                dto = Dto.fromJson(event.data(), RegisteredUserDto.class);
            if (null != mCallback)
                mCallback.onRegisterCallback(event.code(), event.msg(), dto);
        }
    }

    public interface RegisterCallback {

        void onRegisterCallback(int code, String msg, RegisteredUserDto data);
    }
}
